//
//  NewsModel.m
//  MicroNews
//
//  Created by tongcheng on 15/10/26.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "NewsModel.h"
#import "NetworkTools.h"
#import "NSObject+SY.h"

@implementation NewsModel

-(instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if(self){
        self.iid = dict[@"id"];//
        self.c_id = dict[@"c_id"]; //分类ID
        self.title = dict[@"title"];//
        self.thumb = dict[@"thumb"];//
        self.addtime = dict[@"addtime"];//
        self.source = dict[@"source"]; //来源
        self.commont = dict[@"commont"]; //评论
        self.timedesc = dict[@"timedesc"]; //1个月前
        
    }
    
    return self;
}

+(instancetype)newsWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}


+(NSMutableArray *)didNews:(NSMutableArray *)json
{
    NSMutableArray *arrayM = [NSMutableArray array];
    
    if (![json isKindOfClass:[NSNull class]]) {
        for (NSDictionary *dict in json) {
            
            [arrayM addObject:[NewsModel newsWithDict:dict]];
        }
        
        return arrayM;
    }
    return nil;
}

//- (NSString *)description {
//    
//    NSArray *properties = [self.class loadProperties];
//    NSDictionary *dict = [self dictionaryWithValuesForKeys:properties];
//    
//    return [NSString stringWithFormat:@"<%@ : %p> %@",self.class,self,dict];
//}

// 加载网络数据
+ (void)loadDataWithURLString:(NSString *)urlStr finished:(void (^)(NSArray *))finished{
    
    NSAssert(finished != nil, @"必须传完成回调");
    
    [[NetworkTools sharedNetworkTools] GET:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        NSArray *array = responseObject[responseObject.keyEnumerator.nextObject];
        //        NSLog(@"%@",array);
        NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:array.count];
        
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [tempArray addObject:[self objectWithDict:obj]];
        }];
//                NSLog(@"%@",tempArray);
        
        finished(tempArray.copy);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
//        NSLog(@"数据加载失败");
    }];
}



@end
